﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Timers;
using System.Threading;

namespace BoAddonBiz_FreightForwarder.Custom
{
    public class CustomsResponse
    {

        string _DocEntry = string.Empty;
        string _InOut = string.Empty;
        string _FormUID = string.Empty;
        Thread thread;
        public void CheckResponseFile(string TaskId, string DocEntry, string InOut, string FormUID)
        {
            SAPBoAddon.B1Addon.B1Addon.SBOApp.SetStatusBarMessage("准备启动线程……",SAPbouiCOM.BoMessageTime.bmt_Long, false);
            CheckXmlFile cxf = new CheckXmlFile();
            cxf.TaskId = TaskId;
            _DocEntry = DocEntry;
            _InOut = InOut;
            _FormUID = FormUID;
            cxf.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(cxf_PropertyChanged);
            thread = new Thread(new ThreadStart(cxf.Check));
            thread.Start();
            SAPBoAddon.B1Addon.B1Addon.SBOApp.SetStatusBarMessage("准备已启动!", SAPbouiCOM.BoMessageTime.bmt_Long, false);

        }

        void cxf_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var cxf = (sender as CheckXmlFile);
            if (cxf.isSendResult && cxf.isReceiveResult1 && cxf.isReceiveResult2)
            {
                if (cxf.result.flag)
                {
                    SAPbobsCOM.Recordset rs = SAPBoAddon.B1Addon.B1Addon.SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                    string TableName = _InOut == "I" ? "@AVA_FF_OCTC" : "@AVA_FF_OATC";
                    //添加"申报日期"的回写，2015年3月9日 16:18:31 by WangPeng
                    rs.DoQuery("UPDATE [" + TableName + "] SET U_Status = 'R',U_ReportingDate=GetDate() Where DocEntry = " + _DocEntry); 
                    SAPbouiCOM.Form oForm = SAPBoAddon.B1Addon.B1Addon.SBOApp.Forms.Item(_FormUID);
                    if (oForm.Mode != SAPbouiCOM.BoFormMode.fm_FIND_MODE)
                        oForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                    oForm.Items.Item("3").Enabled = true;
                    oForm.Items.Item("3").Specific.Value = _DocEntry;
                    oForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Collapsed);
                    SAPBoAddon.B1Addon.B1Addon.SBOApp.MessageBox("导入成功！");
                }
                else
                {
                    SAPBoAddon.B1Addon.B1Addon.SBOApp.MessageBox("出错啦……\r\n" + cxf.result.message);
                }
                if (thread.IsAlive)
                    thread.Abort();
            }
        }
    }
}
